package whf.framework.security.entity;

import java.util.Iterator;
import java.util.Set;

import whf.framework.entity.AbstractEntity;
import whf.framework.entity.TreeNode;
import whf.framework.util.StringUtils;
import whf.framework.util.Utils;

/**
 * 安全系统的角色
 * @author wanghaifeng
 * 
 */
public class Role extends AbstractEntity implements TreeNode<Role>{
	/**
	 * 父角色,角色可以继承,子角色可以拥有父角色的所有权限,但是如果继承了acegi,在配置过程这个体现不出来
	 * 一般的没必要使用这个
	 * @property Role:parent
	 */
	private Role parent;
	
	/**
	 * 角色所包含的权限
	 * @property Set<Permission>:permissions
	 */
	private Set<Permission> permissions = Utils.newHashSet();
	
	/**
	 * 角色包含的用户
	 * @property Set<User>:users
	 */
	private Set<User> users = Utils.newHashSet();
	
	/**
	 * 角色类型，参见字典FMK-ROLE-TYPE
	 */
	private String type;
	
	/* (non-Javadoc)
	 * @see whf.framework.auth.entity.IRole#getParent()
	 */
	public Role getParent(){
		return this.parent;
	}
	
	/* (non-Javadoc)
	 * @see whf.framework.auth.entity.IRole#setParent(whf.framework.auth.entity.Role)
	 */
	public void setParent(Role role){
		this.parent = role;
	}

	/**
	 * @return Returns the leaf.
	 */
	public boolean isLeaf() {
		return false;
	}

	/**
	 * @return Returns the type.
	 */
	public String getType() {
		return type;
	}

	/**
	 * @param type The type to set.
	 */
	public void setType(String type) {
		this.type = type;
	}

	/* (non-Javadoc)
	 * @see whf.framework.auth.entity.IRole#getPermissions()
	 */
	public Set<Permission> getPermissions(){
		return this.permissions;
	}
	
	/* (non-Javadoc)
	 * @see whf.framework.auth.entity.IRole#setPermissions(java.util.Set)
	 */
	public void setPermissions(Set<Permission> perms){
		this.permissions = perms;
	}
	
	public void mergePermissions(Role role){
		this.permissions.addAll(role.getPermissions());
	}
	
	public boolean hasPermission(Permission perm){
		return this.permissions.contains(perm);
	}
	
	/**
	 * 增加权限
	 * @modify wanghaifeng Oct 16, 2006 9:48:09 AM
	 * @param perm
	 */
	public void addPermission(Permission perm){
		this.permissions.add(perm);
	}
	
	/**
	 * 移出权限
	 * @modify wanghaifeng Oct 16, 2006 9:48:18 AM
	 * @param perm
	 */
	public void removePermission(Permission perm){
		this.permissions.remove(perm);
	}

	/**
	 * @return Returns the users.
	 */
	public Set<User> getUsers() {
		return users;
	}

	/**
	 * @param users The users to set.
	 */
	public void setUsers(Set<User> users) {
		this.users = users;
	}
	
	public void addUser(User user){
		this.users.add(user);
	}
	
	public void removeUser(User user){
		for(Iterator it= this.users.iterator(); it.hasNext(); ){
			User u = (User)it.next();
			if(u.idEquals(user)){
				it.remove();
			}
		}
	}
	
	public boolean contains(User user){
		for(Iterator it= this.users.iterator(); it.hasNext(); ){
			User u = (User)it.next();
			if(u.idEquals(user)){
				return true;
			}
		}
		return false;
	}
	
	//
	public final boolean isAdministrator(){
		return StringUtils.equalsIgnoreCase("administrator", this.getName());
	}
	
	/**
	 * @return 判定是否是业务角色
	 */
	public final boolean isBusineseRole(){
		if(StringUtils.isEmpty(this.type)) return false;
		return this.type.indexOf("B-") >= 0;
	}
	
	/**
	 * @return 判定是否是管理人员角色
	 */
	public final boolean isManagerRole(){
		if(StringUtils.isEmpty(this.type)) return false;
		return this.type.indexOf("M-") >= 0;
	}
}
